package com.petHis.project.system.records.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.petHis.framework.aspectj.lang.annotation.Log;
import com.petHis.framework.aspectj.lang.enums.BusinessType;
import com.petHis.project.system.records.domain.MedicalRecords;
import com.petHis.project.system.records.service.IMedicalRecordsService;
import com.petHis.framework.web.controller.BaseController;
import com.petHis.framework.web.domain.AjaxResult;
import com.petHis.common.utils.poi.ExcelUtil;
import com.petHis.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * 诊疗记录Controller
 * 
 * @author petHis
 * @date 2025-02-16
 */
@Controller
@RequestMapping("/system/records")
public class MedicalRecordsController extends BaseController
{
    private String prefix = "system/records";

    @Autowired
    private IMedicalRecordsService medicalRecordsService;

    @RequiresPermissions("system:records:view")
    @GetMapping()
    public String records()
    {
        return prefix + "/records";
    }

    /**
     * 查询诊疗记录列表
     */
    @RequiresPermissions("system:records:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MedicalRecords medicalRecords)
    {
        startPage();

        List<MedicalRecords> list = medicalRecordsService.selectMedicalRecordsList(medicalRecords);
        return getDataTable(list);
    }

    /**
     * 导出诊疗记录列表
     */
    @RequiresPermissions("system:records:export")
    @Log(title = "诊疗记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MedicalRecords medicalRecords)
    {
        List<MedicalRecords> list = medicalRecordsService.selectMedicalRecordsList(medicalRecords);
        ExcelUtil<MedicalRecords> util = new ExcelUtil<MedicalRecords>(MedicalRecords.class);
        return util.exportExcel(list, "诊疗记录数据");
    }

    /**
     * 新增诊疗记录
     */
    @RequiresPermissions("system:records:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存诊疗记录
     */
    @RequiresPermissions("system:records:add")
    @Log(title = "诊疗记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MedicalRecords medicalRecords, HttpServletRequest request)
    {
        return toAjax(medicalRecordsService.insertMedicalRecords(medicalRecords,request));
    }

    /**
     * 修改诊疗记录
     */
    @RequiresPermissions("system:records:edit")
    @GetMapping("/edit/{recordId}")
    public String edit(@PathVariable("recordId") Long recordId, ModelMap mmap)
    {
        MedicalRecords medicalRecords = medicalRecordsService.selectMedicalRecordsByRecordId(recordId);
        mmap.put("medicalRecords", medicalRecords);
        return prefix + "/edit";
    }

    /**
     * 修改保存诊疗记录
     */
    @RequiresPermissions("system:records:edit")
    @Log(title = "诊疗记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody MedicalRecords medicalRecords)
    {
        return toAjax(medicalRecordsService.updateMedicalRecords(medicalRecords));
    }

    /**
     * 删除诊疗记录
     */
    @RequiresPermissions("system:records:remove")
    @Log(title = "诊疗记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(medicalRecordsService.deleteMedicalRecordsByRecordIds(ids));
    }
}
